/**
 * 关联附件重复校验：
 * 1、先在已选择的附件列表中筛选出重复的项
 * 2、若存在已上传的附件列表，再对比已选择的附件列表是否在已上传的附件列表中有重复的项
 * @param {Array} selectedAttachmentList - 已选择的附件列表
 * @param {Array} uploadedAttachmentList - 已上传的附件列表
 * @returns {Array} - 重复的附件列表
 */
export const validateAttachment = (selectedAttachmentList, uploadedAttachmentList) => {
  const repeatedAttachmentList = [];

  // 第一步
  const uniqueSet = new Set();
  for (const item of selectedAttachmentList) {
    const { filename } = item;

    if (!uniqueSet.has(filename)) {
      uniqueSet.add(filename);
      continue;
    }

    if (uniqueSet.has(filename) && !repeatedAttachmentList.includes(filename)) {
      repeatedAttachmentList.push(filename);
    }
  }

  // 第二步
  if (uploadedAttachmentList.length) {
    for (const Item of selectedAttachmentList) {
      const { filename: Filename } = Item;

      for (const item of uploadedAttachmentList) {
        const { filename } = item;

        if (filename === Filename && !repeatedAttachmentList.includes(filename)) {
          repeatedAttachmentList.push(filename);
          break;
        }
      }
    }
  }

  return repeatedAttachmentList;
}; 
